import json
with open('5.txt', 'w', encoding='utf-8') as txt:
    with open('WordsData/TOEFL.json', 'r', encoding='utf-8') as f:
        data = json.load(f)
        for j in range(len(data)):
            word = data[j]['headWord']
            phone = ''
            if 'usphone' in data[j]['content']['word']['content']:
                phone = '[US]' + data[j]['content']['word']['content']['usphone']
            if 'ukphone' in data[j]['content']['word']['content']:
                if phone is not '':
                    phone = phone + '\n'
                phone = phone + '[UK]'+ data[j]['content']['word']['content']['ukphone']
            string = word + '\n-\n' +phone +'\n--\n'
            if 'syno' in data[j]['content']['word']['content']:
                syno = data[j]['content']['word']['content']['syno']['synos']
                for x in syno:
                    string = string + x['pos']+'\n'+x['tran']+'\n'+(','.join(str(i['w']) for i in x['hwds'])) + '\n'
            elif 'trans' in data[j]['content']['word']['content']:
                for x in data[j]['content']['word']['content']['trans']:
                    if 'pos' in x:
                        string = string + x['pos']+'\n'
                    if 'tranCn' in x:
                        string = string + x['tranCn']+'\n'
            txt.write(string+"---\n")
# with open('CET4luan_1.json', 'r', encoding='utf-8') as f:
#     data = json.load(f)
#     print('synos' in data[0]['content']['word']['content']['syno'])
#     print('syno' in data[0]['content']['word']['content']['syno'])